package com.group.model;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.group.util.NoteException;
import com.group.util.WeiXinConf;
import com.group.util.WeiXinUtils;

/**
 * 微信返回的token对象
 * 
 * @author zhaolei
 * 
 */
public class WeiXinToken {
	private static final Logger logger = LoggerFactory.getLogger(WeiXinToken.class);

	private String access_token;

	private Integer expires_in;

	private String refresh_token;

	private String openid;

	private String scope;

	public WeiXinToken() {

	}

	public WeiXinToken(String code) {
		if (StringUtils.isEmpty(code))
			throw new NoteException("code is empty!!!");
		if (code.equals("a")) {
			this.setOpenid("oUim0uIbCMFjLt8XHD1JntKlwobY");
			return;
		}
		fromCode(code);
	}

	/**
	 * 根据code填充微信的accesstoken
	 * 
	 * @param code
	 */
	public void fromCode(String code) {
		// token获取3次
		for (int i = 0; i < 3; i++) {
			String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WeiXinConf.APPID + "&secret=" + WeiXinConf.SECRET + "&code=" + code + "&grant_type=authorization_code";
			String responseString = WeiXinUtils.get(url);
			logger.info("code获取token响应:" + responseString + " , code = " + code);
			fill(responseString);
			if (getOpenid() == null) {
				logger.info("code获取token发生错误时候的返回结果:" + responseString);
				continue;
			} else {
				break;
			}
		}
		// 3次没拿到token 抛出异常
		if (getOpenid() == null)
			throw new NoteException("3次获取token全部失败, code:" + code);

	}

	/**
	 * 根据json填充自身
	 * 
	 * @param ResponseString
	 */
	public void fill(String ResponseString) {
		JSONObject accessToken = JSONObject.parseObject(ResponseString);
		this.setAccess_token(accessToken.getString("access_token"));
		this.setExpires_in(accessToken.getInteger("expires_in"));
		this.setOpenid(accessToken.getString("openid"));
		this.setRefresh_token(accessToken.getString("refresh_token"));
		this.setScope(accessToken.getString("scope"));
	}

	/**
	 * 根据refresh_token刷新自身
	 * 
	 * @throws IOException
	 */
	public void refresh() throws IOException {
		if (refresh_token == null)
			return;

		String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + WeiXinConf.APPID + "&grant_type=refresh_token&refresh_token=" + refresh_token;

		String responseString = WeiXinUtils.get(url);

		fill(responseString);
	}

	public String getAccess_token() {
		return access_token;
	}

	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}

	public Integer getExpires_in() {
		return expires_in;
	}

	public void setExpires_in(Integer expires_in) {
		this.expires_in = expires_in;
	}

	public String getRefresh_token() {
		return refresh_token;
	}

	public void setRefresh_token(String refresh_token) {
		this.refresh_token = refresh_token;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

}
